package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.PackageDishMapper;
import com.sky.mapper.PackageMapper;
import com.sky.result.PageResult;
import com.sky.service.PackageService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.MarshalException;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class PackageServiceImpl implements PackageService {
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private PackageDishMapper packageDishMapper;

    @Override
    public void updatePackage(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        packageMapper.updatePackage(setmeal);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),
                setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = packageMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addPackage(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        packageMapper.addPackage(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        packageDishMapper.addPackageDish(setmealDishes);
    }

    @Override
    public void status(String status, Long id) throws Exception {
        List<Integer> dishStatus = packageMapper.getDishStatus(id);
        for (Integer o : dishStatus) {
            if (0 == o){
                throw new Exception(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        packageMapper.status(status,id);
    }

    @Override
    public void deletePackage(List<Integer> ids) throws Exception {
        List<Integer> status = packageMapper.getById(ids);
        for (Integer i : status) {
            if (1 == i){
                throw new Exception("套餐已启售，无法删除");
            }
        }
        packageMapper.deletePackage(ids);
        packageMapper.deleteSetmealDish(ids);
    }

    @Override
    public SetmealVO getPackageById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = packageMapper.getPackageById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(Collections.unmodifiableList
                (packageDishMapper.getPackageById(id)));
        return setmealVO;
    }
}
